# -*- coding:utf-8 -*-

from flask import Flask, render_template

app = Flask(__name__, instance_relative_config=True,
            template_folder='templates', 
            static_folder='static')

app.config.from_object('config') # 根目录先config.py 文件
app.config.from_pyfile('config.py') # instance目录下config.py

from flask_mongoengine import MongoEngine, MongoEngineSessionInterface

db = MongoEngine(app)
db.session_interface = MongoEngineSessionInterface(db)

from flask_bootstrap import Bootstrap

bootstrap = Bootstrap(app)

from flask_cache import Cache

cache = Cache(app, config={ 'CACHE_TYPE': 'redis',
                            'CACHE_REDIS_HOST': app.config['CACHE_REDIS_HOST'],
                            'CACHE_REDIS_PORT': app.config['CACHE_REDIS_PORT']
                            })

from celery import Celery

celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)


from flask_login import LoginManager

login_manager = LoginManager(app)
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'

from flask_mail import Mail

mail = Mail(app)

from .api import api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api')

from .email import email
app.register_blueprint(email)

from .admin import admin
app.register_blueprint(admin)

from .login import login
app.register_blueprint(login)

from .customer import customer
app.register_blueprint(customer)


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def sever_error(e):
    return render_template('500.html'), 500

@app.errorhandler(403)
def page_403(e):
    return render_template('403.html'), 403